package gui.action;

import generated.domain.room.Room;
import gui.Activator;
import gui.IImageKeys;
import gui.ResourceManager;
import gui.view.GlobalChatView;
import gui.view.RoomView;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import vworld.Client;

public class ChangeRoomAction extends Action {

	public final static String ID = "gui.action.ChangeRoomAction";
	public int X, Y;

	public ChangeRoomAction(String _actionName) {
		// super(provider, text);
		super(_actionName, Action.AS_DROP_DOWN_MENU);
		createMenu();
	}

	public void createMenu() {
		this.setToolTipText("Change room");

		this.setMenuCreator(new IMenuCreator() {
			public Menu getMenu(Control control) {
				Menu menu = new Menu(control);
				MenuItem elem = new MenuItem(menu, SWT.FLAT);
				elem.setText("left");
				elem.setImage(ResourceManager.getPluginImage(Activator
						.getDefault(), IImageKeys.LEFT));
				elem.setEnabled(Client.CURRENT_ROOM.westGate);
				elem.addSelectionListener(new SelectionListener() {
					public void widgetDefaultSelected(SelectionEvent e) {
					}

					public void widgetSelected(SelectionEvent e) {
						Y = Client.CURRENT_ROOM.location.y - 1;
						run("left");
					}
				});

				elem = new MenuItem(menu, SWT.FLAT);
				elem.setText("top");
				elem.setImage(ResourceManager.getPluginImage(Activator
						.getDefault(), IImageKeys.UP));
				elem.setEnabled(Client.CURRENT_ROOM.northGate);
				elem.addSelectionListener(new SelectionListener() {
					public void widgetDefaultSelected(SelectionEvent e) {
					}

					public void widgetSelected(SelectionEvent e) {
						X = Client.CURRENT_ROOM.location.x - 1;
						run("top");
					}
				});

				elem = new MenuItem(menu, SWT.FLAT);
				elem.setText("bottom");
				elem.setImage(ResourceManager.getPluginImage(Activator
						.getDefault(), IImageKeys.DOWN));
				elem.setEnabled(Client.CURRENT_ROOM.southGate);
				elem.addSelectionListener(new SelectionListener() {
					public void widgetDefaultSelected(SelectionEvent e) {
					}

					public void widgetSelected(SelectionEvent e) {
						X = Client.CURRENT_ROOM.location.x + 1;
						run("bottom");
					}
				});

				elem = new MenuItem(menu, SWT.FLAT);
				elem.setText("right");
				elem.setImage(ResourceManager.getPluginImage(Activator
						.getDefault(), IImageKeys.RIGHT));
				elem.setEnabled(Client.CURRENT_ROOM.eastGate);
				elem.addSelectionListener(new SelectionListener() {
					public void widgetDefaultSelected(SelectionEvent e) {
					}

					public void widgetSelected(SelectionEvent e) {
						Y = Client.CURRENT_ROOM.location.y + 1;
						run("right");
					}
				});

				control.setMenu(menu);
				return menu;
			}

			public Menu getMenu(Menu menu) {
				return menu;
			}

			public void dispose() {
			}
		});
	}

	public void run(String _direction) {
		Client.CALL_BACK_REF.roomName("");
		Client.ROOM_SERVICE.exitRoom(Client.CALL_BACK_REF,
				Client.CONNECTED_USER.avatar, Client.CURRENT_ROOM.name);

		Room tempRoom = null;
		for (Room r : Client.CURRENT_WORLD.rooms) {
			if (r.location.x == X && r.location.y == Y) {
				tempRoom = r;
				break;
			}
		}

		if (tempRoom != null) {
			updateGlobalChatView(tempRoom.name);
			updateRoomView(tempRoom.name);
			Client.CALL_BACK_REF.roomName(tempRoom.name);
			Client.CURRENT_ROOM = Client.ROOM_SERVICE.joinRoom(
					Client.CALL_BACK_REF, Client.CONNECTED_USER.avatar,
					tempRoom.name);
		} else {
			Status status = new Status(
					IStatus.ERROR,
					Activator.PLUGIN_ID,
					0,
					"The room at location [" + X + "," + Y + "] doesn't exist!",
					null);
			ErrorDialog.openError(Display.getCurrent().getActiveShell(),
					"Can not change room", "The room can not be reached!",
					status);
		}
	}

	private void updateGlobalChatView(String roomName) {
		IWorkbenchWindow window = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow();
		IWorkbenchPage page = window.getActivePage();
		IViewPart globalChatView = page.findViewReference(GlobalChatView.ID)
				.getView(false);

		if (globalChatView != null) {
			((GlobalChatView) globalChatView).changeTitle(roomName + " "
					+ "Global Chat");
			((GlobalChatView) globalChatView).getMessagesArea().setText("");
		}
	}

	private void updateRoomView(String roomName) {
		IWorkbenchWindow window = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow();
		IWorkbenchPage page = window.getActivePage();
		IViewPart roomView = page.findViewReference(RoomView.ID).getView(false);

		if (roomView != null) {
			((RoomView) roomView).changeTitle(roomName);
		}
	}
}
